package KK.EMVC.Control
{
	import KK.GameBase.IEventBase;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.GameBase.MessageQueue;
	import KK.System.ArgumenError;
	import KK.System.System;
	
	import flash.utils.Dictionary;
	
	public class Control implements IControl
	{
		static public const VERSION:int=System.VERSION;
		static protected const E_SERIOUS:int=0xFF;//严格检查机制 
		static protected const E_NORMAL:int=0;//普通 检查 
		static protected const E_NOTICE:int=1;//只trace消息 
		static protected const E_ARGUMEN:int=2;//异常抛出
		
		protected var _event:IMessageQueue;//消息队列
		
		protected var msgList:Dictionary; //消息列表
		
		static private const EventInstance:IMessageQueue=new MessageQueue();
		
		protected var check:int;
		
		public function Control()
		{
			//TODO: implement function
			init();
		}
		
		private function init():void{
			 
			check=E_SERIOUS;
			msgList=new Dictionary();
			!_event&&BindEvent(GetMessageQueue());
		}
		
		
		/**
		 * 获取消息队列 
		 * @return 
		 * 
		 */
		static public function GetMessageQueue():IMessageQueue{
			return EventInstance;
		}
		
		public function BindEvent(_event:IMessageQueue):Boolean
		{
			//TODO: implement function
			this._event=_event;
			return true;
		}
		public function Update(event:MSGEvent):void
		{
			//TODO: implement function
			if(msgList&&msgList[event.type]is Function)
			{
				msgList[event.type](event);
			}
		}
		/**
		 * 发布消息 
		 * @param msg
		 * 
		 */ 
		protected function Publish(event:MSGEvent):void{

			if(this._event){
				this._event.Publish(event);
			}else{
				ArgumenError.checkArgumen("publish event faild.");
			}
		}
		protected function RemoveMessage(type:String):void{
			if(this._event){
				delete msgList[type];
				this._event.RemoveMessage(type,this);
			}else{
				ArgumenError.checkArgumen("Remove Message event faild.");
			}
		}
		protected function AttentionMessage(type:String,callback:Function=null):void
		{
			if(this._event){
				msgList[type]=callback;
				this._event.AttentionMessage(type,this);
			}else{
				ArgumenError.checkArgumen("AttentionMessage event faild.");
				
			}
		} 
		
		
		
		public function trace(...args):void{
			System.DebugMSG.apply(null,["Mark Trace : "].concat(args));
		}
	}
}